<html>
<head><meta charset="utf-8"><title>fn pointer Debug vs Stacked Borrows · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html">fn pointer Debug vs Stacked Borrows</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="163954238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/163954238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#163954238">(Apr 23 2019 at 01:53)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">test_fn</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;Hello!&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Debug)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">FunctionHolder</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">function</span>: <span class="nc">fn</span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">FunctionHolder</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">FunctionHolder</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">function</span>: <span class="nc">test_fn</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FunctionHolder</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">f0</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0080]: constant evaluation error: attempted to do invalid arithmetic on pointers that would leak base addresses, e.g., comparing pointers into different allocations
   --&gt; /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/cmp.rs:978:52
    |
978 |                 fn ge(&amp;self, other: &amp;$t) -&gt; bool { (*self) &gt;= (*other) }
    |                                                    ^^^^^^^^^^^^^^^^^^^ attempted to do invalid arithmetic on pointers that would leak base addresses, e.g., comparing pointers into different allocations
    |
    = note: inside call to `std::cmp::impls::&lt;impl std::cmp::PartialOrd for usize&gt;::ge` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/num.rs:53:30
    = note: inside call to `&lt;core::fmt::num::LowerHex as core::fmt::num::GenericRadix&gt;::fmt_int::&lt;usize&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/num.rs:135:46
    = note: inside call to `core::fmt::num::&lt;impl std::fmt::LowerHex for usize&gt;::fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/mod.rs:2024:19
    = note: inside call to `&lt;*const () as std::fmt::Pointer&gt;::fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/ptr.rs:2634:17
    = note: inside call to `std::ptr::&lt;impl std::fmt::Debug for fn()&gt;::fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/mod.rs:1914:81
    = note: inside call to `&lt;&amp;fn() as std::fmt::Debug&gt;::fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/builders.rs:146:17
    = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/result.rs:639:22
    = note: inside call to `std::result::Result::&lt;(), std::fmt::Error&gt;::and_then::&lt;(), [closure@DefId(2/1:2376 ~ core[6458]::fmt[0]::builders[0]::{{impl}}[2]::field[0]::{{closure}}[0]) 0:&amp;mut &amp;mut std::fmt::DebugStruct, 1:&amp;&amp;str, 2:&amp;&amp;dyn std::fmt::Debug]&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/builders.rs:130:23
note: inside call to `std::fmt::DebugStruct::field` at src/main.rs:5:10
   --&gt; src/main.rs:5:10
    |
5   | #[derive(Debug)]
    |          ^^^^^
    = note: inside call to `&lt;FunctionHolder as std::fmt::Debug&gt;::fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/fmt/mod.rs:1016:17
    = note: inside call to `std::fmt::write` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/io/mod.rs:1279:15
    = note: inside call to `&lt;std::io::StdoutLock as std::io::Write&gt;::write_fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/io/stdio.rs:533:9
    = note: inside call to `&lt;std::io::Stdout as std::io::Write&gt;::write_fmt` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/io/stdio.rs:786:9
    = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/thread/local.rs:299:16
    = note: inside call to `std::thread::LocalKey::&lt;std::cell::RefCell&lt;std::option::Option&lt;std::boxed::Box&lt;dyn std::io::Write + std::marker::Send&gt;&gt;&gt;&gt;::try_with::&lt;[closure@DefId(1/1:1027 ~ std[82ff]::io[0]::stdio[0]::print_to[0]::{{closure}}[0]) 0:&amp;std::fmt::Arguments, 1:&amp;fn() -&gt; std::io::Stdout], std::result::Result&lt;(), std::io::Error&gt;&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/io/stdio.rs:780:18
    = note: inside call to `std::io::stdio::print_to::&lt;std::io::Stdout&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/io/stdio.rs:802:5
note: inside call to `std::io::_print` at &lt;::std::macros::println macros&gt;:2:3
   --&gt; src/main.rs:18:5
    |
18  |     println!(&quot;{:?}&quot;, f0);
    |     ^^^^^^^^^^^^^^^^^^^^^
    = note: inside call to `main` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:34
    = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:53
    = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:293:40
    = note: inside call to `std::panicking::try::do_call::&lt;[closure@DefId(1/1:1834 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:289:5
    = note: inside call to `std::panicking::try::&lt;i32, [closure@DefId(1/1:1834 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe]&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panic.rs:388:9
    = note: inside call to `std::panic::catch_unwind::&lt;[closure@DefId(1/1:1834 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:25
    = note: inside call to `std::rt::lang_start_internal` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:5
    = note: inside call to `std::rt::lang_start::&lt;()&gt;`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
</pre></div>


<p>/cc <span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="164003281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164003281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164003281">(Apr 23 2019 at 16:03)</a>:</h4>
<p>This has nothing to do with Stacked Borrows. You cannot print pointers in Miri.</p>



<a name="164024880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164024880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164024880">(Apr 23 2019 at 20:03)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span>  basically Miri does not actually pick integer base addresses for allocations, it keeps them abstract to make execution deterministic. so whenever program behavior actually depends on that choice, Miri aborts.</p>



<a name="164024897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164024897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164024897">(Apr 23 2019 at 20:03)</a>:</h4>
<p>see <a href="https://github.com/rust-lang/miri/issues/224" target="_blank" title="https://github.com/rust-lang/miri/issues/224">https://github.com/rust-lang/miri/issues/224</a> for the way we plan to fix this, eventually</p>



<a name="164042882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164042882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164042882">(Apr 24 2019 at 00:42)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  Sure, but is Miri "right" or "wrong" here?</p>



<a name="164059582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164059582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164059582">(Apr 24 2019 at 07:15)</a>:</h4>
<p>the message doesn't mean "this is UB", it means "this program does stuff nit supported by Miri"</p>



<a name="164059586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164059586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164059586">(Apr 24 2019 at 07:15)</a>:</h4>
<p>(we have a long-standing open issue to make this classification clearer)</p>



<a name="164265335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn%20pointer%20Debug%20vs%20Stacked%20Borrows/near/164265335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/fn.20pointer.20Debug.20vs.20Stacked.20Borrows.html#164265335">(Apr 26 2019 at 14:16)</a>:</h4>
<p>Ohhhhhhhh. TIL</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>